package com.googlecode.jpocketmod.templates.pages;

import java.awt.Graphics;
import java.awt.Image;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

/**
 *
 * @author David Sosby
 */
public class StaticTextContent extends PageContent  {
	private String value = "";
	
	public StaticTextContent() {}

	public String getValue() {		return value;	}
	public void setValue(String value) {		this.value = value;	}
	
	public Image drawContentToImage(Image img, Map<String,String> options)  {
		String populatedString = value;
		Iterator<String> stringOptions = getAvailableOptions().iterator();
		while ( stringOptions.hasNext() )  {
			String stringOption = stringOptions.next();
			String optionValue = options.get(stringOption);
			if ( optionValue == null ) optionValue = "";
			populatedString.replaceAll("$$" + stringOption + "$$", optionValue);
		}
		
		Graphics g = img.getGraphics();
		g.drawString(populatedString, this.getPositionX(), this.getPositionY());
		
		return img;
	}
	
	public List<String> getAvailableOptions()  {
		Vector<String> options = new Vector<String>();
		
		int frontIndex = 0;
		int backIndex = value.length()-1;
		while ( (frontIndex = value.indexOf("$$")) >= 0 )  {
			backIndex = value.indexOf("$$", frontIndex+2);
			if ( backIndex >= 0 )  {
				String optionName = value.substring(frontIndex+2, backIndex);
				if ( !options.contains(optionName) )	options.add(optionName);
			}
		}
		
		return options;
	}
}
